<script setup>
/*
  @/pages/my/my.vue 我的页面
  主要功能：
    登录
    历史足迹
    用户信息修改
*/
// import { ref } from 'vue';
import { onShow } from "@dcloudio/uni-app";
import customNavBarVue from "@/components/custom-nav-bar/custom-nav-bar.vue";
import { apiGetHistory } from "@/api/user.js";
import { useUserStore } from "@/store/user.js";
import { storeToRefs } from "pinia";
import { ref, watchEffect } from "vue";
//是否同意用户协议
const isAgree = ref(false)
const handlAgree = () => {
  console.log(isAgree.value);
  isAgree.value = !isAgree.value

}
// 历史记录列表
const historyList = ref([])
const userStore = useUserStore();
const { token, userInfo } = storeToRefs(userStore);
//浏览历史请求参数
// 定义 historyParams 为响应式引用
const historyParams = ref({
  userId: userInfo.value.id,
  history: userInfo.value.history
});
watchEffect(() => {
  historyParams.value = {
    userId: userInfo.value.id,  // userInfo 是响应式的
    history: userInfo.value.history
  };
});
onShow(async () => {
  if (token.value) {
    console.log("onshow-history-", historyParams.value);
    await getHistory(historyParams.value);
  }
});

const handleLogin = async () => {
  uni.showLoading({
    title: "加载中",
    mask: true,
  });
  // 1. 获取 code
  const { code } = await uni.login({ provider: "weixin" });
  if (!code) {
    uni.showToast({ title: "登录失败", icon: "none" });
    return;
  }
  await userStore.fetchLogin({ code });
  uni.showToast({ title: "登录成功", icon: "none" });
  // console.log("fun-history-1", historyParams.value); 
  // console.log('fun-history-2',userInfo.value.id,userInfo.value.history)
  await getHistory(historyParams.value); //登录之后请求历史记录


  setTimeout(() => {
    uni.hideLoading();
  }, 100);
};

//获取用户历史足迹
const getHistory = async (data = {}) => {
  const res = await apiGetHistory(data);
  historyList.value = res
};

//个人信息设置
const toSetInfo = () => {
  console.log('setting');
  uni.navigateTo({ url: '/pages/setUserInfo/setUserInfo' })
}

//我的收藏
const toFavorite = () => {
  console.log('favorite');

}
</script>

<template>
  <view class="unLogin" v-if="!token">
    <view class="unLoginBox">
      <view class="handleLogin" @click="handleLogin">
        <uni-icons type="weixin" size="30" color="#fff"></uni-icons>
        <text>微信一键登录</text>
      </view>
      <view class="agreement">
        <!-- 使用 label 包裹，点击文字或自定义框都能触发 checkbox -->
        <label class="custom-checkbox" @click="handlAgree">
          <!-- 1. 隐藏原生 checkbox -->
          <checkbox value="cb" :checked="isAgree" class="native-checkbox" />

          <!-- 2. 创建视觉替身 -->
          <view class="checkbox-icon">
            <text class="checkbox-checkmark" v-if="isAgree">✓</text>
          </view>

          <text class="agreement-text">
            同意<text style="color: #0468BC;">用户服务协议</text>和<text style="color: #0468BC;">隐私条款</text>
          </text>
        </label>
      </view>
    </view>
  </view>
  <view v-else class="login-container pageBg">
    <customNavBarVue title="我的" :isSearch="false"></customNavBarVue>
    <!-- 个人信息 -->
    <view class="avatar">
      <image v-if="!token" @click="handleLogin"
        src="https://test-4688.oss-cn-beijing.aliyuncs.com/73562ede-609d-4e3d-8f78-446f58ffb09e.png" mode="aspectFill">
      </image>
      <image v-if="token" :src="userInfo.avatar" mode="aspectFill"></image>
      <view v-if="!token" @click="handleLogin" class="nickName">未登录</view>
      <view class="nickName" v-if="token">{{ userInfo.nickName }}</view>
      <!-- 修复判断逻辑 -->
    </view>
    <!-- 历史足迹 -->
    <view class="my-history">
      <view class="title">历史足迹</view>
      <scroll-view class="history-box" scroll-x show-scrollbar="false" enable-flex>
        <view class="history-items-container">
          <template v-for="item in historyList" :key="item">
            <view class="item" v-if="item.coverImage">
              <view class="coverImg">
                <image :src="item.coverImage" mode="aspectFill" />
              </view>
              <text class="title">
                {{ item.title1 }}
              </text>
              <!-- {{ item.id }} -->
              <view class="time">
                <uni-dateformat :date="item.viewTime" format="yyyy-MM-dd"></uni-dateformat>
              </view>
            </view>
          </template>
        </view>
      </scroll-view>
    </view>
    <!-- 个人功能列表 -->
    <view class="my-list">
      <!-- 我的推荐/作品 -->
      <view class="my-list-item">
        <view class="left">
          <uni-icons type="chatboxes" size="25" color="#79c6ec"></uni-icons>
          <text class="txt">管理帖子</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>
      <!-- 我感兴趣 -->
      <view class="my-list-item">
        <view class="left">
          <uni-icons type="heart" size="25" color="#79c6ec"></uni-icons>
          <text class="txt">偏好设置</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>

      <!-- 我的收藏 -->
      <view class="my-list-item" @click="toFavorite">
        <view class="left">
          <uni-icons type="star" size="25" color="#79c6ec"></uni-icons>
          <text class="txt">我的收藏</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>
      <!-- 个人信息设置 -->
      <view class="my-list-item" @click="toSetInfo">
        <view class="left">
          <uni-icons type="auth" size="25" color="#79c6ec"></uni-icons>
          <text class="txt">个人信息设置</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>
    </view>
    <view class="my-list">
      <view class="my-list-item">
        <view class="left">
          <uni-icons type="chat" size="25" color="#FB7299"></uni-icons>
          <text class="txt">联系我们</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>
      <view class="my-list-item">
        <view class="left">
          <uni-icons type="flag" size="25" color="#FB7299"></uni-icons>
          <text class="txt">小程序说明</text>
        </view>
        <view class="right">
          <uni-icons type="right" size="15" color="#aaa"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.unLogin {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .unLoginBox {
    .handleLogin {
      background-color: #03DA6B;
      display: flex;
      gap: 10rpx;
      align-items: center;
      padding: 15rpx 60rpx;
      border-radius: 999px;

      text {
        color: #fff;
        font-size: 35rpx;
      }
    }

    .agreement {
      /* 根据你的布局需求调整 */
      margin: 20rpx;
    }

    .custom-checkbox {
      display: flex;
      align-items: center;
      font-size: 24rpx;
      color: #666;
    }

    /* 1. 隐藏原生 checkbox 的关键样式 */
    .native-checkbox {
      position: absolute;
      opacity: 0;
      width: 0;
      height: 0;
    }

    /* 2. 视觉替身：checkbox 的外壳 */
    .checkbox-icon {
      width: 32rpx;
      /* 你想要的宽度 */
      height: 32rpx;
      /* 你想要的高度 */
      border: 2rpx solid #ccc;
      border-radius: 6rpx;
      /* 圆角 */
      margin-right: 16rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      transition: all 0.3s ease;
      /* 过渡动画，让状态变化更平滑 */
    }

    /* 3. 选中状态的样式 */
    .native-checkbox:checked+.checkbox-icon {
      background-color: #0468BC;
      /* 选中后的背景色 */
      border-color: #0468BC;
      /* 选中后的边框色 */
    }

    /* 对勾的样式 */
    .checkbox-checkmark {
      color: white;
      font-size: 24rpx;
      font-weight: bold;
    }

    .agreement-text {
      /* 确保文字不会因为太长而换行到 checkbox 下面 */
      flex: 1;
    }
  }
}

.login-container {
  padding: 0 30rpx;

  .avatar {
    padding: 30rpx 0;
    display: flex;
    gap: 40rpx;
    align-items: center;

    image {
      width: 100rpx;
      height: 100rpx;
      border-radius: 50rpx;
    }

    .nickName {
      font-size: 40rpx;
    }
  }

  .my-history {
    width: 100%;
    /* 建议用 padding 控制高度，而非固定 height */
    padding: 20rpx;
    padding-top: 5rpx;
    box-sizing: border-box;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    /* my-history 作为 flex 容器，垂直排列 title 和 scroll-view */
    display: flex;
    flex-direction: column;
    gap: 10rpx;
    /* 增加一点间距 */

    .title {
      font-size: 25rpx;
      margin-bottom: 10rpx;
    }

    .history-box {
      width: 100%;
      white-space: nowrap;

      .history-items-container {
        display: flex;
        align-items: center;
        gap: 20rpx;

        .item {
          display: flex;
          flex-direction: column;
          // align-items: center;
          // justify-content: center;
          // width: 150rpx;
          // height: 60rpx;
          // background-color: skyblue;
          // border-radius: 30rpx;
          margin-right: 15rpx;
          flex-shrink: 0;

          .coverImg {
            width: 170rpx;
            height: 140rpx;

            image {
              width: 100%;
              height: 100%;
              border-radius: 10rpx;
            }
          }

          .title {
            font-size: 22rpx;
            width: 140rpx;
            text-align: left;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          .time {
            font-size: 20rpx;
            color: #bebebe;
          }
        }
      }
    }

  }

  .my-list {
    display: flex;
    flex-direction: column;
    box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.05);
    border-radius: 10rpx;
    border: 1px solid #eee;
    margin-top: 25rpx;
    animation: fadeInContainer 0.6s ease both;

    .my-list-item {
      padding: 20rpx 20px;
      border-bottom: 1px solid #eee;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .left {
        display: flex;
        align-items: center;
        gap: 20rpx;

        .txt {
          font-size: 28rpx;
          color: #666;
        }
      }

      .right {
        color: #aaa;
      }
    }
  }
}

/* 整体容器淡入 */
@keyframes fadeInContainer {
  from {
    opacity: 0;
    transform: translateY(100rpx);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 每个 item 的上浮入场 */
@keyframes fadeUp {
  from {
    opacity: 0;
    transform: translateY(40rpx) scale(0.96);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
</style>
